package com.chinacache.maint.workorder.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.chinacache.maint.commons.orm.hibernate.Page;
import com.chinacache.maint.commons.web.struts2.CRUDActionSupport;
import com.chinacache.maint.workorder.entity.Field;
import com.chinacache.maint.workorder.entity.FieldAttribute;
import com.chinacache.maint.workorder.entity.FieldClass;
import com.chinacache.maint.workorder.entity.FieldClassToField;
import com.chinacache.maint.workorder.entity.Flow;
import com.chinacache.maint.workorder.entity.FlowClass;
import com.chinacache.maint.workorder.entity.FlowFieldClass;
import com.chinacache.maint.workorder.service.FieldAttributeManager;
import com.chinacache.maint.workorder.service.FlowClassManager;

@SuppressWarnings("serial")
public class FlowClassAction extends CRUDActionSupport<FlowClass> {
	private FlowClassManager flowClassManager;
	private FieldAttributeManager fieldAttributeManager;
	private FlowClass entity;
	private Integer id;
	private Page<FlowClass> page = new Page<FlowClass>();
	Map<List, Map> view = null;

	// private String condition;

	@Override
	public String delete() throws Exception {
		if (entity.getFlows().size() > 0) {
			this.msg = "该流程下存在子流程，请先删除子流程！";
			return list();
		}
		flowClassManager.deleteFlowClass(id);
		return RELOAD;
	}

	@Override
	public FlowClass getModel() {
		return entity;
	}

	@Override
	public Page<FlowClass> getPage() {
		return page;
	}

	@Override
	public String list() throws Exception {
		page = flowClassManager.getAllFlowClass(page);
		return SUCCESS;
	}

	@Override
	public void prepare() throws Exception {
		if (id != null) {
			entity = flowClassManager.getFlowClass(id);
		} else {
			entity = new FlowClass();
		}
	}

	@Override
	public String save() throws Exception {
		flowClassManager.saveFlowClass(entity);
		return RELOAD;
	}

	public void setFlowClassManager(FlowClassManager flowClassManager) {
		this.flowClassManager = flowClassManager;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String view() {
		view = new LinkedHashMap<List, Map>();
		int i = 0;
		for (Flow flow : entity.getFlows()) {
			i++;
			Map<String, List> fas = new LinkedHashMap<String, List>();
			for (FlowFieldClass ffc : flow.getFlowFieldClasss()) {
				FieldClass fieldClass = ffc.getFieldClass();
				for (FieldClassToField fctField : fieldClass
						.getFieldClassFields()) {
					List<List> fasList = new ArrayList<List>();
					Field field = fctField.getField();
					Set<FieldAttribute> faSet = field.getFieldAttibutes();
					List<Integer> groupIds = new ArrayList<Integer>();
					for (FieldAttribute fa : faSet) {
						if (!groupIds.contains(fa.getGroupId())) {
							groupIds.add(fa.getGroupId());
						}
					}
					for (Integer groupId : groupIds) {
						List faByGroupIdList = fieldAttributeManager
								.getFieldAttributeByGroupId(groupId);
						fasList.add(faByGroupIdList);
					}
					fas.put(field.getName(), fasList);
				}
				List<String> aList = new ArrayList<String>();
				aList.add(flow.getName());
				aList.add(flow.getMemo());
				aList.add("第" + i + "步");
				view.put(aList, fas);
			}
		}
		return "view";
	}

	// public void setCondition(String condition) {
	// this.condition = condition;
	// }

	public Map<List, Map> getView() {
		return view;
	}

	public void setView(Map<List, Map> view) {
		this.view = view;
	}

	public void setFieldAttributeManager(
			FieldAttributeManager fieldAttributeManager) {
		this.fieldAttributeManager = fieldAttributeManager;
	}

}
